package com.koron.css2.newProjectCenter.bean;

import cn.hutool.core.collection.CollectionUtil;
import com.koron.css2.baseConfig.bean.BaseBean;
import com.koron.css2.newProjectCenter.mapper.UserWaterCardMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.DateUtils;
import com.koron.util.Tools;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 水卡管理数据类
 *
 * @author 杨嘉慧
 * @date 2024/6/24
 */
@Data
public class UserWaterCardBean extends BaseBean {

    /**
     * 开户后自动生成制卡单
     */
    public static final String AUTO_REGISTER_MAKE_AFTER_OPEN = "1";

    /**
     * 出发自动生成制卡单的变更字段名称
     */
    private static final List<String> AUTO_REGISTER_MAKE_CHANGE_FIELD_NAME = new ArrayList<>(Arrays.asList("客户名称", "客户地址"));

    /**
     * 变更后自动生成制卡单
     */
    public static final String AUTO_REGISTER_MAKE_AFTER_USERCHG = "1";

    /**
     * 未制卡状态
     */
    public static final int NOT_MAKE_STATUS = 0;

    /**
     * 制卡中状态
     */
    public static final int MAKING_STATUS = 1;

    /**
     * 已领卡状态
     */
    public static final int RECEIVED_STATUS = 2;

    /**
     * 制卡单主键di
     */
    private String id;

    /**
     * 用户编号
     */
    private String userNo;

    /**
     * 登记人账号
     */
    private String registrantAccount;

    /**
     * 登记人名称
     */
    private String registrantName;

    /**
     * 登记时间
     */
    private String registerTime;

    /**
     * 制卡人账号
     */
    private String makerOperatorAccount;

    /**
     * 制卡人名称
     */
    private String makerOperatorName;

    /**
     * 制卡时间
     */
    private String makeTime;

    /**
     * 领卡操作人
     */
    private String receiveOperatorAccount;

    /**
     * 领卡人名称
     */
    private String receiveOperatorName;

    /**
     * 领卡时间
     */
    private String receiveTime;

    /**
     * 状态,0未制卡,1制卡中,2已领卡
     */
    private Integer status;

    /**
     * 设置登记属性
     *
     * @param userInfo
     */
    public void setRegisterInfo(UserInfoBean userInfo, String registerTime) {
        setId(Tools.getObjectId());
        setStatus(NOT_MAKE_STATUS);
        setRegistrantAccount(userInfo.getUserInfo().getAcount());
        setRegistrantName(userInfo.getUserInfo().getName());
        setRegisterTime(StringUtils.isBlank(registerTime)
                ? DateUtils.parseDate(new Date(), "yyyy-MM-dd HH:mm:ss") : registerTime);
    }

    /**
     * 设置制卡属性
     *
     * @param userInfo
     */
    public void setMakeInfo(UserInfoBean userInfo) {
        setStatus(MAKING_STATUS);
        setMakerOperatorAccount(userInfo.getUserInfo().getAcount());
        setMakerOperatorName(userInfo.getUserInfo().getName());
        setMakeTime(DateUtils.parseDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 设置领卡属性
     *
     * @param userInfo
     */
    public void setReceiveInfo(UserInfoBean userInfo) {
        setStatus(RECEIVED_STATUS);
        setReceiveOperatorAccount(userInfo.getUserInfo().getAcount());
        setReceiveOperatorName(userInfo.getUserInfo().getName());
        setReceiveTime(DateUtils.parseDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 登记单个制卡单
     *
     * @param factroy
     * @param userInfo
     * @param userNo
     * @param checkIfExistNotReceive 检查是否存在未领卡单开关
     */
    public static void registerSingleUserWaterCard(SessionFactory factroy, UserInfoBean userInfo, String userNo, boolean checkIfExistNotReceive) {
        UserWaterCardMapper cardMapper = factroy.getMapper(UserWaterCardMapper.class);
        registerMultipleUserWaterCard(cardMapper, userInfo, new ArrayList<>(Arrays.asList(userNo)), checkIfExistNotReceive);
    }

    /**
     * 登记多个制卡单
     *
     * @param factroy
     * @param userInfo
     * @param userNos
     * @param checkIfExistNotReceive 检查是否存在未领卡单开关
     */
    public static void registerMultipleUserWaterCard(SessionFactory factroy, UserInfoBean userInfo, List<String> userNos, boolean checkIfExistNotReceive) {
        UserWaterCardMapper cardMapper = factroy.getMapper(UserWaterCardMapper.class);
        registerMultipleUserWaterCard(cardMapper, userInfo, userNos, checkIfExistNotReceive);
    }

    /**
     * 登记多个制卡单
     *
     * @param cardMapper
     * @param userInfo
     * @param userNos
     * @param checkIfExistNotReceive 检查是否存在未领卡单开关
     */
    public static void registerMultipleUserWaterCard(UserWaterCardMapper cardMapper, UserInfoBean userInfo, List<String> userNos, boolean checkIfExistNotReceive) {
        //整理制卡单数据
        List<UserWaterCardBean> userWaterCardList = Lists.newArrayList();
        //保证所有制卡单的登记时间一致
        String registerTime = DateUtils.parseDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        //如果检查未领卡，则只对那些不存在未领卡的用户进行制卡单登记
        if (checkIfExistNotReceive) {
            List<String> notReceivedUserList = cardMapper.notReceivedListByUserNos(userNos);
            userNos.removeAll(notReceivedUserList);
        }
        if (CollectionUtil.isNotEmpty(userNos)) {
            for (String userNo : userNos) {
                UserWaterCardBean userWaterCard = new UserWaterCardBean();
                userWaterCard.setUserNo(userNo);
                userWaterCard.setRegisterInfo(userInfo, registerTime);
                userWaterCardList.add(userWaterCard);
            }
            //制卡单存入数据库
            cardMapper.multipleUserWaterCardAdd(userWaterCardList);
        }
    }

    /**
     * 校验修改的用户信息中是否包含触发自动生成制卡单的修改字段
     *
     * @param fieldList
     */
    public static boolean checkContainAutoRegisterField(List<String> fieldList) {
        if (CollectionUtil.containsAny(fieldList, AUTO_REGISTER_MAKE_CHANGE_FIELD_NAME)) {
            return true;
        }
        return false;
    }
}
